import * as Router from 'koa-router';
// import { IRouterContext } from '../core/IRouterContext';
import { IRouterContext } from 'koa-router';
import motorService from '../service/MotorService';
const motorCtr: Router = require('koa-router')();
motorCtr.prefix('/api/motor')
import loginUtil from './../core/LoginUtils';
import logger from '../lib/Log4js';


/**
 * 上汽登录接口
 * 身份认证 username uf01 pwd admin@123
 * vpn用户名/密码：liding/tq09y9g1
 */
motorCtr.post('/login', async (ctx: IRouterContext, next: any) => {
    console.log('进入登录接口__________________');
    logger.infoLogger().info('登陆');
    let rq: any = ctx.request
    let data = rq.body;
    let message:any = await motorService.login(data);

    ctx.status = 200;
    ctx.body = message;
    await next()
})

motorCtr.get('/test', async (ctx: IRouterContext, next: any) => {

    ctx.status = 200;
    ctx.body = {ok:"ok"};
    await next()
})

/**
 * 仓库管理系统
 * 登录者数据请求
 * 参数 authorization = userToken
 */
motorCtr.post('/show_module', async (ctx: IRouterContext, next: any) => {
    // let authorization :any = ctx.request.body.authorization;//ctx.header.authorization;
    
    let rq: any = ctx.request
    let data = rq.body;
    let result:any = await motorService.show_module(data.authorization);
    ctx.status = 200;
    ctx.body = result;
    await next()
})

/**
 * 判断用户session
 */
motorCtr.post('/check_session', async (ctx: IRouterContext, next: any) => {
    
    let authorization = ctx.headers.authorization;
    console.log('headers__________________',ctx.headers.authorization);
    
    let ss = await loginUtil.checkUser(authorization);
    console.log('判断用户session__________________',ss);
    ctx.status = 200;
    await next();
})


export default motorCtr;